<template>
  <div class="login">
    <!-- 相对定位中国移动图标 -->
    <div class="china-mobile-img">
      <img :src="chinaMobileLogo" />
    </div>
    <div class="login-form-box">
      <div class="login-logo">
        <img :src="loginLogo" />
      </div>
      <div class="login-content">
        <!-- <div class="login-person"><img :src="smallBgPic" /></div> -->
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
          <div style="text-align: center;">
            <div class="title">智慧管理平台</div>
            <div class="login-notice">输入您的账号密码登录</div>
          </div>
          <el-form-item prop="username">
            <div class="login-username">
              <span>登录账号</span>
              <el-input v-model.trim="loginForm.username" type="text" auto-complete="off" placeholder="">
              </el-input>
                <!-- <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" /> -->
            </div>
          </el-form-item>
          <el-form-item prop="password">
            <div class="login-username">
              <span style="width: 70px;">密码</span>
              <el-input
                v-model="loginForm.password"
                type="password"
                auto-complete="off"
                placeholder=""
                @keyup.enter.native="handleLogin"
              >
                <!-- <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" /> -->
              </el-input>
            </div>
          </el-form-item>
          <el-form-item prop="code">
            <div class="login-username">
              <span style="width: 70px;">验证码</span>
              <el-input
                v-model="loginForm.code"
                auto-complete="off"
                placeholder=""
                style="width: 63%"
                @keyup.enter.native="handleLogin"
              >
                <!-- <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" /> -->
              </el-input>
              <div class="login-code">
                <img :src="codeUrl" @click="getCode" class="login-code-img"/>
              </div>
            </div>
          </el-form-item>
          <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">
            <span style="color: #999;">记住我的登录状态</span></el-checkbox>
          <el-form-item style="width:100%;">
            <el-button
              :loading="loading"
              size="medium"
              type="success"
              style="width:100%;"
              @click.native.prevent="handleLogin"
              class="login-submit-btn"
            >
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
          </el-form-item>
        </el-form>
        </div>
      </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { getRouters } from '@/api/menu'
import router from '../router'
import { encrypt, decrypt } from '@/utils/jsencrypt'
import { getSubsystemByUserId,checkSubsystemByUserId,getHaiKangApi } from '@/api/login'
export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      cookiePassword: "",
      loginLogo: require("@/assets/image/login_logo.png"),
      chinaMobileLogo: require("@/assets/image/china_mobile.png"),
      // smallBgPic: require("@/assets/image/login_small_bgpng.png"),
      loginForm: {
        username: "admin",
        password: "admin123",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "用户名不能为空" }
        ],
        password: [
          { required: true, trigger: "blur", message: "密码不能为空" }
        ],
        code: [{ required: true, trigger: "change", message: "验证码不能为空" }]
      },
      loading: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.codeUrl = "data:image/gif;base64," + res.img;
        this.loginForm.uuid = res.uuid;
      });
    },
    init() {
      if (screenfull.enabled) {
        screenfull.on('change', this.change)
      }
    },
    getRoutesFunc(item) {
      // this.init()
       var params = { subsystemId:item.subsystemId }
       console.log('params', params)
      this.$store.dispatch('tagsView/delAllViews').then(({ visitedViews }) => {
      })
      checkSubsystemByUserId(params).then((res)=>{
        if(res.code == 200 && res.result == true){
          Cookies.set('subSystemLogoNum', item.num)
          Cookies.set('subSystemTitle', item.subsystemName)
          Cookies.set('subSystemLogo', item.subsystemImg)
          Cookies.set('sidebarStatus', 1)
          this.$store.dispatch('tagsView/delAllViews').then(({ visitedViews }) => {
          })
          Cookies.set('hrefMenuType', 1)
          Cookies.set('subsystemId', item.subsystemId)
          if(item.openMenu == 1){
            console.log('22', item.subsystemUrl)
            this.$store.dispatch('GenerateRoutes', item).then(accessRoutes => {
            // 测试 默认静态页面
            // store.dispatch('permission/generateRoutes', { roles }).then(accessRoutes => {
              // 根据roles权限生成可访问的路由表
              router.addRoutes(accessRoutes) // 动态添加可访问路由表
            })
            this.$router.push({
              path: "/index"
            })
          }
        }
      })
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      // console.log('是吗')
      let _this = this
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store
            .dispatch("Login", this.loginForm)
            .then(() => {
              console.log('登录是否成功')
            //   this.$router.push({
            //   path: "/home"
            // })
              Cookies.set('subsystemId', 1)
              _this.getRoutesFunc({
                createBy: null,
                createName: "管理员",
                createTime: "2020-08-10 14:34:05",
                delFlag: 0,
                openMenu: 1,
                orderNum: 8,
                params: {},
                remark: null,
                searchValue: null,
                substatus: "0",
                subsystemId: 1,
                subsystemImg: "http://36.148.24.62:8001/stage-api/profile/upload/2020/09/03/8822f015-719e-41dd-8ee8-767c47ac0c26.png",
                subsystemName: "统一集成门户系统",
                subsystemUrl: "/index",
                updateBy: null,
                updateTime: "2020-11-06 08:43:05",
              })
            })
            .catch(() => {
              this.loading = false;
              this.getCode();
            });
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/image/home_bg.jpg");
  background-size: cover;
}
.title {
  margin: 0px auto 15px auto;
  text-align: center;
  color: #34a02c;
  font-size: 26px;
}
.login-notice{
  color: #bebebe;
  margin-bottom: 15px;
  font-size: 12px;
}

.login-form {
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  // width: 33%;
  height: 38px;
  // float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}

.login-form-box{
    // width: 380px;
    padding: 25px 80px;
    // background-color: #ffffff;
    background-color:rgba(255,255,255,0.8);
    border-radius: 10px;
    filter:Alpha(opacity=80);
    // box-shadow: 0 2px 12px 0 #4aa8e2;
    // padding-right: 80px;
    // padding-bottom: 50px;
    //opacity: 0.8;
    // filter:alpha(opacity=50);
}
.login-form-box .login-logo{
  position: absolute;
    top: 40px;
    left: 150px;
    z-index: 99;
    width: 260px;
    height: 55px;
    display: flex;
}
.login-form-box .login-logo img{
  width: 100%;
  height: 100%;
}
.china-mobile-img{
  position: absolute;
  right: 10%;
  bottom: 5%;
  z-index: 99;
  width: 150px;
  height: 45px;
}
.china-mobile-img img{
  width: 100%;
  height: 100%;
}
.login-content{
  width: 100%;
  overflow: hidden;
}
.login-content .login-person{
  float: left;
  width: 50%;
  margin-top: 50px;
}
.login-content .login-person img{
  width: 360px;
  height: 260px;
}
.login-content .login-form{
  float: right;
  width: 100%;
}
.login-submit-btn{
  background: #34a02c !important;
  width: 100% !important;
  border-color: #34a02c !important;
  // box-shadow: 0px 0px 15px #4aa8e2 !important;
  border-radius: 15px !important;
}
.login-submit-btn:hover{
  border-color: snow;
}
.login-username{
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  border-bottom: 1px solid #bfbfbf;
}
.login-username input{
  border: 0px !important;
  background-color: rgba(255,255,255,0) !important;
}
.login-username span{
  display: inline-block;
  word-break: keep-all;
}
</style>
